<?php
/**
 * Created by PhpStorm.
 * User: qufan
 * Date: 2016/12/12 0012
 * Time: 22:36
 */

namespace Home\Controller;


class MapController extends HomeController
{

    protected $pi = 3.1415926535897932384626;
    protected $a = 6378245.0;
    protected $ee = 0.00669342162296594323;

    /**
    WGS84转GCj02 (CGCS2000转火星坐标)

    @param lon 经度
    @param lat 纬度
     */
    protected function wgs84togcj02(&$lon, &$lat){

        if ($this->out_of_china($lon, $lat)){
            return;
        }else{

            $dlat = $this->transformlat($lon - 105.0, $lat - 35.0);
            $dlng = $this->transformlng($lon - 105.0, $lat - 35.0);
            $radlat = $lat / 180.0 * $this->pi;
            $magic = sin($radlat);
            $magic = 1 - $this->ee * $magic * $magic;

            $sqrtmagic = sqrt($magic);
            $dlat = ($dlat * 180.0) / (($this->a * (1-$this->ee))/($magic * $sqrtmagic) * $this->pi);
            $dlng = ($dlng * 180.0) / ($this->a / $sqrtmagic * cos($radlat) * $this->pi);

            $mglat = $lat + $dlat;
            $mglng = $lon + $dlng;

            $lon = sprintf("%.8f", $mglng);
            $lat = sprintf("%.8f", $mglat);
        }
    }

    protected function transformlat($lon, $lat) {
        $ret = -100.0 + 2.0 * $lon + 3.0 * $lat + 0.2 * $lat * $lat + 0.1 * $lon * $lat + 0.2 * sqrt(abs($lon));
        $ret += (20.0 * sin(6.0 * $lon * $this->pi) + 20.0 * sin(2.0 * $lon * $this->pi)) * 2.0 / 3.0;
        $ret += (20.0 * sin($lat * $this->pi) + 40.0 * sin($lat / 3.0 * $this->pi)) * 2.0 / 3.0;
        $ret += (160.0 * sin($lat / 12.0 * $this->pi) + 320 * sin($lat * $this->pi / 30.0)) * 2.0 / 3.0;

        return $ret;
    }

    protected function transformlng( $lon, $lat) {
        $ret = 300.0 + $lon + 2.0 * $lat + 0.1 * $lon * $lon + 0.1 * $lon * $lat + 0.1 * sqrt(abs($lon));
        $ret += (20.0 * sin(6.0 * $lon * $this->pi) + 20.0 * sin(2.0 * $lon * $this->pi)) * 2.0 / 3.0;
        $ret += (20.0 * sin($lon * $this->pi) + 40.0 * sin($lon / 3.0 * $this->pi)) * 2.0 / 3.0;
        $ret += (150.0 * sin($lon / 12.0 * $this->pi) + 300.0 * sin($lon / 30.0 * $this->pi)) * 2.0 / 3.0;
        return $ret;
    }

    protected function out_of_china($lon, $lat){
        return ($lon < 72.004 || $lon>137.8347)||(($lat < 0.8293||$lat>55.8271)||false);
    }

    public function index(){
        $this->meta_title = '电子地图管理';
        $this->display();
    }

    public function mapReport($ids = null){
        $field = "mp.user_id,mp.longitude, mp.latitude, u.real_name,u.sex,u.phone,u.IDCard, u.position, r.role_name, u.IMEI, u.icon, 'internal' as class ";
        $where['mp.state'] = 1;
        if(!empty($ids)){
            if(is_string($ids)){
                $ids = explode(',',$ids);
                $where['mp.user_id'] = array('in', $ids);
            }
        }
        if(!empty($this->uids)){
            $where['_string'] = "mp.user_id in(".implode(',', $this->uids).")";
        }
        $records = M('MapReport')->alias('mp')->join('user u on mp.user_id=u.user_id')->join('role r on u.role_id=r.role_id')->field($field)->where($where)->select();
        if($records){
            $data['records'] = $records;
            $this->sucessJson($data);
        }else{
            $this->errorJson('没有上报数据');
        }
    }

    public function datumReport1($tname, $class){
        $field = "*, '{$class}' as class ";
        $where['state'] = 1;
        $records = M($tname)->field($field)->where($where)->select();
        if($records){
            $this->sucessJson($records);
        }else{
            $this->errorJson('没有上报数据');
        }
    }
    /**
    *add : 添加 物质企业表区别 用level进行连接表
    *add : 在物质企业表等级处进行数值与名称替换
    *user: 边个懂
    *time: 20170518
    **/
    public function datumReport($class_id){
        $classToTable = C('CLASS_TO_TABLE');
        if (isset($classToTable[$class_id])){
            $tableName = $classToTable[$class_id]['name'];
            if(!empty($this->uids)){
                $map['_string'] = "a.orgcode in(".implode(',', $this->codes).")";
            }
            $map['a.state'] = 1;
            $field = 'a.*, type_name';
            if ( $tableName == 'DatumMaterial') {
                $record = M($tableName)->alias('a')->join('datum_dic_type ddt on a.level=ddt.type_code','left')->field($field)->where($map)->select();
            }else{
                $record = M($tableName)->alias('a')->join('datum_dic_type ddt on a.type=ddt.type_code','left')->field($field)->where($map)->select();
            }
            if($record){
                foreach ($record as $key => $val){
                    $lng = $record[$key]['longitude'];
                    $lat = $record[$key]['latitude'];
                    $this->wgs84togcj02($lng, $lat);
                    $record[$key]['longitude'] = $lng;
                    $record[$key]['latitude'] = $lat;
                }

                $fields = M($tableName)->getDbFields();
                $fields = getFieldsDescr($fields);
                if ($tableName == 'DatumMaterial') {
                    for ($i=0; $i <count($fields) ; $i++) { 
                        if ($fields[$i]['field'] == 'level'){
                              $fields[$i]['field'] = 'type_name'; 
                        }
                    }
                }else{
                    $fields[] = array('field'=>'type_name','title'=>'类型');
                }
                $data['records'] = $record;
                $data['fields'] = $fields;

                $this->sucessJson($data);
            }
        }
        $this->errorJson('没有上报数据');
    }

    public function search($search, $mapSelect){
        $map_array = C('MAP_BUTTON');
        $records = array();
        $where = array();
        empty($search) or $where['name'] = array('like', "%{$search}%");
        if(!empty($this->uids)){
            $where['_string'] = "orgcode in(".implode(',', $this->codes).")";
        }
        if($mapSelect == 0){
            foreach ($map_array as $key=>$val){
                $tname = $val['table'];
                if($tname == 'MapReport'){
                    continue;
//                    $field = 'mp.longitude, mp.latitude, u.real_name,u.sex,u.phone,u.IDCard, u.position, r.role_name, u.IMEI, u.icon ';
//                    $data = M('MapReport')->alias('mp')->join('user u on mp.user_id=u.user_id')->join('role r on u.role_id=r.role_id')->field($field)->where($where)->select();
                }else{
                    $data = M($tname)->where($where)->select();
                }

                foreach ($data as $item){
                    $item['class'] = $val['class'];
                    $records[] = $item;
                }
            }
        }else{
            if(isset($map_array[$mapSelect])){
                $val=$map_array[$mapSelect];
                $tname = $val['table'];
                if($tname == 'MapReport'){
                    if(!empty($this->uids)){
                        $where['_string'] = "mp.user_id in(".implode(',', $this->uids).")";
                    }
                    $field = 'mp.longitude, mp.latitude, u.real_name,u.sex,u.phone,u.IDCard, u.position, r.role_name, u.IMEI, u.icon ';
                    $data = M('MapReport')->alias('mp')->join('user u on mp.user_id=u.user_id')->join('role r on u.role_id=r.role_id')->field($field)->where($where)->select();
                }else{
                    $data = M($tname)->where($where)->select();
                }
                foreach ($data as $item){
                    $item['class'] = $val['class'];
                    $records[] = $item;
                }
            }else{
                $this->errorJson('数据有误');
            }
        }
        $data['records'] = $records;
        $this->sucessJson($data);
    }

    public function queryDatumEnclosure($datum_dic_class, $datum_id){
        $where['class_id'] = $datum_dic_class;
        $where['relation_id']= $datum_id;
        $record = M('DatumEnclosure')->where($where)->select();
        if($record){
            $this->sucessJson($record);
        }else{
            $this->errorJson('没有附件');
        }
    }


}